<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
namespace core\member;
defined('BY_JYA') or exit('error');
//提现
class cash{
	function __construct(){
		$setting=uni_setting('cash');
		$this->setting=$setting;
	}
	function check_open($isred=false){
		if($isred){
			return $this->setting['openred']?true:false;
		}else{
			return $this->setting['open']?true:false;
		}
	}
	/*
		检查提现金额是否符合要求！
	*/
	function check_cash_fee($mid,$fee,$merchid=0){
		if(!$this->check_open()){
			exi('系统不支持提现！','error');
		}
		if(!$fee){
			exi('提现失败，未设置提现金额！','error');
		}
		if(is_numeric($fee)){
		}else{
			exi('提现提现金额必须是数字！','error');
		}
		if($merchid){
			$member=model('jy_merch/member')->getdetail($merchid,$mid);
		}else{
			$member=model('core/member')->detail($mid,array('credit2'));
		}
		if(!$member){
			exi('提现失败，指定会员不存在！','error');
		}
		if($this->setting['min'] && $fee<$this->setting['min']){
			exi("提现失败，提现金额需大于{$this->setting['min']}元！",'error');
		}
		if($member['credit2']<$fee){
			exi('提现失败，会员账户余额不足！当前余额：'.$member['credit2'],'error');
		}
		if($merchid){
			model('jy_merch/member')->member_credit($merchid,$mid,'credit2',$fee,false,'core','会员提现支出');
			model('jy_merch/member')->fz_credit2($merchid,$mid,$fee,'会员提现冻结','core',1);
		}else{
			model('core/member')->cut_credit2($mid,$fee,'会员提现支出','core');
			model('core/member')->fz_credit2($mid,$fee,'会员提现冻结','core');
		}
		return true;
	}
	function check_cash_jifen($mid,$jifen,$money=0,$merchid=0){
		if(!$this->setting['openjifen']){
			exi('系统不支持积分提现！','error');
		}
		if(!$jifen){
			exi('提现失败，未设置提现金额！','error');
		}
		if(is_numeric($jifen)){
		}else{
			exi('提现积分数量必须是数字！','error');
		}
		if($merchid){
			$member=model('jy_merch/member')->getdetail($merchid,$mid);
		}else{
			$member=model('core/member')->detail($mid,array('credit1'));
		}
		if(!$member){
			exi('提现失败，指定会员不存在！','error');
		}
		if($this->setting['min'] && $money<$this->setting['min']){
			exi("提现失败，提现金额需大于{$this->setting['min']}元！",'error');
		}
		if($member['credit1']<$jifen){
			exi('提现失败，会员账户余额不足！当前余额：'.$member['credit1'],'error');
		}
		if($merchid){
			model('jy_merch/member')->member_credit($merchid,$mid,'credit1',$jifen,false,'core','会员提现支出');
			model('jy_merch/member')->fz_credit($merchid,$mid,$jifen,'会员提现冻结','core',1);
		}else{
			model('core/member')->cut_credit1($mid,$jifen,'会员提现支出','core');
			model('core/member')->fz_credit($mid,$jifen,'会员提现冻结','core',1);
		}
		return true;
	}
	function check_cash_xunibi($mid,$jifen,$money=0,$type=0,$merchid=0){
		$mid=$mid?:$_SESSION['uid'];
		if(!$jifen){
			exi('提现失败，未设置提现数量！','error');
		}
		if(is_numeric($jifen)){
		}else{
			exi('提现数量必须是数字！','error');
		}
		if($merchid){
			$member=model('jy_merch/member')->getdetail($merchid,$mid);
		}else{
			$member=model('core/member')->detail($mid,array('credit'.$type));
		}
		if(!$member){
			exi('提现失败，指定会员不存在！','error');
		}
		if($this->setting['min'] && $money<$this->setting['min']){
			exi("提现失败，提现金额需大于{$this->setting['min']}元！",'error');
		}
		if($member['credit'.$type]<$jifen){
			exi('提现失败，会员账户余额不足！当前余额：'.$member['credit'.$type],'error');
		}
		if($merchid){
			model('jy_merch/member')->member_credit($merchid,$mid,'credit'.$type,$jifen,false,'core','会员提现支出');
			model('jy_merch/member')->fz_credit($merchid,$mid,$jifen,'会员提现冻结','core',$type);
		}else{
			model('core/member')->member_credit($mid,'credit'.$type,$jifen,false,'core','会员提现支出');
			model('core/member')->fz_credit($mid,$jifen,'会员提现冻结','core',$type);
		}
		return true;
	}
	/*
		执行提现
	*/
	function pay($cash_id){
		$cash=pdo_get('core_member_cash',array('id'=>$cash_id));
		if(!$cash){
			exi('提现打款失败，提现订单不存在！','error');
		}
		if($cash['status']){
			exi('提现打款失败，该提现订单已打款！','error');
		}
		if(!$cash){
			exi('提现打款失败，提现订单不存在！','error');
		}
		//$this->check_cash_fee($cash['mid'],$cash['money']);
		$params=array(
			'money'=>$cash['cash_money'],
			'remark'=>'会员余额提现',
			'merchid'=>$cash['merchid'],
		);
		if($cash['merchid']){
			$pay_out_id=model('jy_merch/member')->pay_out($cash['mid'],$params);
		}else{
			$pay_out_id=model('core/member')->pay_out($cash['mid'],$params);
		}
		$update=array(
			'status'=>1,
			'pay_out_id'=>$pay_out_id,
		);
		pdo_update('core_member_cash',$update,array('id'=>$cash['id']));
		$credit_type=$cash['credit_type']?:2;
		if($credit_type==2){
			$money=$cash['money'];
		}else{
			$money=$cash['credit_fee'];
		}
		model('core/member')->thaw_credit($cash['mid'],$money,'会员提现成功','core',$credit_type);
		return true;
	}
	public function apitixian($cash=array()){
		$apiplugin=$this->apiplugin($cash['mid']);
		if($apiplugin){
			model($apiplugin.'/api')->tixian($cash,false,$this->setting);
		}
		return true;
	}
	public function apilog($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		$apiplugin=$this->apiplugin($mid);
		if($apiplugin){
			$log=model($apiplugin.'/api')->getlog($mid,$this->setting);
			$log['apiplugin']=$apiplugin;
		}
		return $log;
	}
	//$ismobile=1;表示核对同手机
	public function getlog($mid=0,$ismobile=0){
		$mid=$mid?:$_SESSION['uid'];
		$apiplugin=$this->apiplugin($mid);
		if($apiplugin){
			$log=pdo_get('core_member_bank',array('plugin_name'=>$apiplugin,'mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'passed'=>1));
			if($ismobile && !$log){
				$mobile=pdo_getcolumn('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),'mobile');
				$log=pdo_get('core_member_bank',array('passed'=>1,'mobile'=>$mobile,'plugin_name'=>$apiplugin,'uniacid'=>$_SESSION['uniacid']));
			}
		}else{
			$log=pdo_get('core_member_bank',array('mid'=>$mid,'uniacid'=>$_SESSION['uniacid'],'passed'=>1));
			if($ismobile && !$log){
				$mobile=pdo_getcolumn('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),'mobile');
				$log=pdo_get('core_member_bank',array('passed'=>1,'mobile'=>$mobile,'uniacid'=>$_SESSION['uniacid']));
			}
		}
		return $log;
	}
	public function apiplugin($mid=0){
		$mid=$mid?:$_SESSION['uid'];
		if($this->setting['huifuapi_cash']==2){
			$apiplugin='jy_huifuapi';
		}elseif($this->setting['huiqi365_cash']==2){
			$apiplugin='jy_huiqi365';
		}else{
			if($this->setting['huiqi365_cash']==1 && $this->setting['huifuapi_cash']){
				$apiplugin='jy_huifuapi';
			}elseif($this->setting['huiqi365_cash']){
				$apiplugin='jy_huiqi365';
			}elseif($this->setting['huifuapi_cash']){
				$apiplugin='jy_huifuapi';
			}
		}
		if($apiplugin){
			model($apiplugin.'/api')->getlog($mid,$this->setting);
		}
		return $apiplugin;
	}
	public function upbank($postdata=array()){
		$mid=$mid?:$_SESSION['uid'];
		if($postdata['id']){
			$log=pdo_get('core_member_bank',array('id'=>$postdata['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		$data=array(
			'sn'=>'SN'.get_ordersn('core_member_bank','sn'),
			'realname'=>$postdata['realname'],
			'cardnum'=>$postdata['cardnum'],
			'bankcard'=>$postdata['bankcard'],
			'mobile'=>$postdata['mobile'],
			'cardimg1'=>$postdata['cardimg1'],
			'cardimg2'=>$postdata['cardimg2'],
			'cardimg3'=>$postdata['cardimg3'],
			'bankname'=>$postdata['bankname'],
			'banktitle'=>$postdata['banktitle'],
			'pcd'=>ijson_encode($postdata['pcd']),
			'post_data'=>ijson_encode($postdata['post_data']),
		);
		if($log['id']){
			pdo_update('core_member_bank',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['id']));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$_SESSION['uid'];
			$data['plugin_name']=$postdata['plugin_name'];
			$data['createtime']=TIMESTAMP;
			pdo_insert('core_member_bank',$data);
			$data['id']=pdo_insertid();
			$log=$data;
		}
		if($postdata['plugin_name']){
			$res_data=model($postdata['plugin_name'].'/api')->checkinfo($data);
			pdo_update('core_member_bank',array('passed'=>$res_data['passed'],'res_data'=>ijson_encode($res_data)),array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['id']));
		}else{
			pdo_update('core_member_bank',array('passed'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$log['id']));
		}
		return $log;
	}
	public function notice_tixian($mid,$fee=0,$typetitle=''){
		$mid=$mid?:$_SESSION['uid'];
		//提现申请通知
		$template_id=cfc('account')->check_tpl_template('OPENTM405485000','core','提现申请通知');
		$userinfo=pdo_get('core_members',array('id'=>$mid),array('fromuser','nickname'));
		$message=array(
			'first'=>array('value'=>'您好,提现申请已经收到',"color" => "#4a5077"),
			'keyword1'=>array('title'=>'昵称','value'=>$userinfo['nickname'],"color" => "#4a5077"),
			'keyword2'=>array('title'=>'时间','value'=>date('Y/m/d H:i:s',time()),"color" => "#4a5077"),
			'keyword3'=>array('title'=>'金额','value'=>$fee,"color" => "#4a5077"),
			'keyword4'=>array('title'=>'提现方式','value'=>$typetitle?:'微信',"color" => "#4a5077"),
			'remark'=>array('value'=>'感谢您的使用！！',"color" => "#4a5077"),
		);
		if(!$url){
			$url=custom_url('core/member.cash_log/',true);
		}
		cfc('account')->sendtwoNotice($mid,$template_id,$message,$url,'notice_tixian','core');
		$this->notice_tixian_admin($fee,$typetitle);
		return true;
	}
	public function notice_tixian_admin($fee=0,$typetitle=''){
		$mid=$mid?:$_SESSION['uid'];
		//提现申请通知管理员
		$setting=uni_setting('cash');
		$notice_mid=$setting['notice_mid'];
		if($notice_mid){
			return false;
		}
		$template_id=cfc('account')->check_tpl_template('OPENTM405485000','core','提现申请通知');
		$userinfo=pdo_get('core_members',array('id'=>$mid),array('fromuser','nickname'));
		$message=array(
			'first'=>array('value'=>'您好,有新的提现申请要处理！',"color" => "#4a5077"),
			'keyword1'=>array('title'=>'昵称','value'=>$userinfo['nickname'],"color" => "#4a5077"),
			'keyword2'=>array('title'=>'时间','value'=>date('Y/m/d H:i:s',time()),"color" => "#4a5077"),
			'keyword3'=>array('title'=>'金额','value'=>$fee,"color" => "#4a5077"),
			'keyword4'=>array('title'=>'提现方式','value'=>$typetitle?:'微信',"color" => "#4a5077"),
			'remark'=>array('value'=>'请及时查看！',"color" => "#4a5077"),
		);
		if($notice_mid){
			cfc('account')->sendtwoNotice($notice_mid,$template_id,$message,$url,'notice_tixian','core');		
		}
		return true;
	}
}